/*
 * 投中信息
 * Copyright 2017 (C) All Rights Reserved.
 */
package cn.studyjava.verify.config.exception;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.studyjava.verify.common.Const;
import cn.studyjava.verify.common.enums.VerifyEnum;
import cn.studyjava.verify.common.result.JsonResult;

/**
 * <p>
 * Description:
 * </p>
 * 
 * @author: zsljava
 * @version 1.0.0
 * <p>
 * History:
 * -----------------------------------------------
 * @Date: 2017年9月6日 上午11:11:37
 * @author: zsljava
 * @version 1.0.0
 * @OP: Create
 * -----------------------------------------------
 * </p>
 *
 * @since
 * @see
 */
@ControllerAdvice
public class VerifyExceptionHandler {
	private Logger log = LoggerFactory.getLogger(VerifyExceptionHandler.class);
	
	@ResponseBody
	@ExceptionHandler(value = RedisConnectionFailureException.class)
	public JsonResult<Object> redisConnectionErrorHandler(HttpServletRequest req, Exception e) {
		e.printStackTrace();
		log.error(VerifyEnum.SERVICE_REIDS_CONNECTION_FAIL.getMsg());
		return new JsonResult<Object>(Const.VERIFY_STATUS_FAIL, VerifyEnum.SERVICE_REIDS_CONNECTION_FAIL.getCode(), VerifyEnum.SERVICE_REIDS_CONNECTION_FAIL.getMsg());
	}
	
	@ResponseBody
	@ExceptionHandler(value = Exception.class)
	public JsonResult<Object> defaultErrorHandler(HttpServletRequest req, Exception e) {
		log.error(e.getMessage());
		return new JsonResult<Object>(Const.VERIFY_STATUS_FAIL, VerifyEnum.VERIFY_AUTH_FAIL.getCode(), e.getMessage());
	}
}
